
////////////////////////////////// Description /////////////////////////////////

// BYTEREADC peripheral model example 'creatingBYTEREADC'


#include "interrupter.h"
/////////////////////////////// Port Declarations //////////////////////////////

BYTEREADCSP_ab32_dataT BYTEREADCSP_ab32_data;
handlesT handles;

/////////////////////////////// Diagnostic level ///////////////////////////////

// Test this variable to determine what diagnostics to output.
// eg. if (diagnosticLevel > 0) bhmMessage("I", "dmac", "Example");

Uns32 diagnosticLevel;

/////////////////////////// Diagnostic level callback //////////////////////////

static void setDiagLevel(Uns32 new) {
    diagnosticLevel = new;
}

///////////////////////////// MMR Generic callbacks ////////////////////////////

static PPM_VIEW_CB(view32) {  *(Uns32*)data = *(Uns32*)user; }

/////////////////////////////////// Net Ports //////////////////////////////////

static void installNetPorts(void) {
	handles.intp = ppmOpenNetPort("intp");
}

//////////////////////////// Memory mapped registers ///////////////////////////

static void installRegisters(void) {

    ppmCreateRegister("byteData",
        "data",
        handles.BYTEREADCSP,
        0,
        1,
        regRd32,
        0,
        view32,
        &(BYTEREADCSP_ab32_data.byteData.value),
        True
    );
}

//////////////////////////////// Bus Slave Ports ///////////////////////////////

static void installSlavePorts(void) {
    handles.BYTEREADCSP = ppmCreateSlaveBusPort("BYTEREADCSP", 33); // 9 is the size of extent to be exposed, needs to be the difference between icmConnectPSEBus highAddress and lowAddress?

}

////////////////////////////////// Constructor /////////////////////////////////

PPM_CONSTRUCTOR_CB(periphConstructor) {
	installSlavePorts();
	installRegisters();
	installNetPorts();
}

///////////////////////////////////// Main /////////////////////////////////////

int main(int argc, char *argv[]) {
    diagnosticLevel = 0;
    bhmInstallDiagCB(setDiagLevel);
    constructor();

	while(1) {
		bhmWaitDelay(1000000000);
		ppmWriteNet(handles.intp, 1);
		bhmPrintf("\nSet interrupt!\n");
	}
	
    destructor();
    return 0;
}

